package com.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blog.bo.BlogArticleBO;
import com.blog.bo.BlogCommentBO;
import com.blog.entity.BlogArticle;
import com.blog.entity.BlogComment;
import com.blog.entity.BlogManager;
import com.blog.service.CommentService;
import com.blog.utils.BusinessException;
import com.blog.utils.TreeNodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author Huangzp
 * @Date 2022/1/7 16:39
 */
@Slf4j
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private BlogCommentBO blogCommentBO;

    @Autowired
    private BlogArticleBO blogArticleBO;

    @Autowired
    private HttpServletRequest request;

    @Override
    public List<BlogComment> getCommentsByArticleId(int articleId) {
        QueryWrapper<BlogComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogComment::getArticleId,articleId);
        List<BlogComment> comments = blogCommentBO.list(queryWrapper);
        return TreeNodeUtil.assembleTree(comments);
    }

    @Override
    public void saveComment(BlogComment comment) throws BusinessException {
        try {
//            if (StringUtils.isEmpty(comment.getNickname())){
//                Random random=new Random();
//                comment.setNickname("游客"+random.nextInt(90)+10);
//            }
            BlogArticle article = blogArticleBO.getById(comment.getArticleId());
            comment.setArticleName(article.getArticleName());
            blogCommentBO.save(comment);
        } catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }
    }

    @Override
    public void deleteComment(Integer id, Integer articleByManagerId) throws BusinessException{
        try {
            //查询当前登录人,是否为文章发表人
            BlogManager managerByArticle = (BlogManager) request.getSession().getAttribute("managerInfo"+articleByManagerId);
            //为null说明当前不是文章发表人
            if (managerByArticle == null){
                //查询评论人id
                BlogComment comment = blogCommentBO.getById(id);
                BlogManager managerByComment = (BlogManager) request.getSession().getAttribute("managerInfo"+comment.getByManagerId());
                if (managerByComment == null){
                    throw new BusinessException("你没有资格删除这条评论");
                }
            }
            blogCommentBO.removeById(id);
        } catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }
    }

    @Override
    public List<BlogComment> getCommentPage(BlogComment comment) {
        QueryWrapper<BlogComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(comment.getNickname()), BlogComment::getNickname, comment.getNickname())
                .orderByDesc(BlogComment::getCreateTime);
        return blogCommentBO.list(queryWrapper);
    }

    @Override
    public void delComment(List<Integer> ids) {
        try {
            blogCommentBO.removeByIds(ids);
        }catch (BusinessException e){
            throw new BusinessException(e.getErrorMessage());
        }
    }
}
